//
//  SEGNewBusinessCircleMainViewModel.m
//  Commom
//
//  Created by 梁学彰 on 2020/6/23.
//

#import "SEGNewBusinessCircleMainViewModel.h"
#import "BusinessCircleRequestManager.h"

@interface SEGNewBusinessCircleMainViewModel ()

@property (nonatomic, copy) NSString *moreTime;
@property (nonatomic, assign) NSInteger totalPage;
@property (nonatomic, assign) BOOL isLoading;
@end

@implementation SEGNewBusinessCircleMainViewModel
- (instancetype)initWithType:(SEGNewBusinessCircleType)type {
    if (self = [super init]) {
        _type = type;
        [self seg_creatListCommand];
    }
    return self;
}

- (void)seg_creatListCommand {
    @weakify(self);
    self.listCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(NSNumber *isRefresh) {
        
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            if (self.isLoading) {
                [subscriber sendNext:nil];
                [subscriber sendCompleted];
                return nil;
            }
            
            NSMutableDictionary *param = [@{@"merchantType": self.selectedType ?: @""} mutableCopy];
            if (!isRefresh.boolValue) {
                param[@"moreTime"] = self.moreTime ?: @"";
                if (self.totalPage <= 1) {
                    [subscriber sendNext:nil];
                    [subscriber sendCompleted];
                    return nil;
                }
            }
            self.isLoading = YES;
            if (self.type == SEGNewBusinessCircleType_Merchants) {
                [BusinessCircleRequestManager requestMerchantListWithParam:param finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                    self.isLoading = NO;
                    [subscriber sendNext:@(0)];
                    NSDictionary* dict = result;
                    
                    //数据返回正常
                    if (isSuccessed) {
                        NSArray *requestData = [[dict objectForKey:@"data"] objectForKey:@"result"];
                        if (isRefresh.boolValue) {
                            self.listData = requestData;
                        } else {
                            self.listData = [self.listData arrayByAddingObjectsFromArray:requestData];
                        }
                        self.moreTime = [[dict objectForKey:@"data"] objectForKey:@"moreTime"];
                        self.totalPage = [[[[dict objectForKey:@"data"] objectForKey:@"pagination"] objectForKey:@"totalPage"] integerValue];
                        
                    } else {
                        [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:100 userInfo:@{NSLocalizedDescriptionKey:errorMessage}]];
                    }
                    [subscriber sendNext:@(1)];
                    [subscriber sendCompleted];
                }];
            }else {
                [BusinessCircleRequestManager requestPrivilegeListWithParam:param finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                    self.isLoading = NO;
                    [subscriber sendNext:@(0)];
                    NSDictionary* dict = result;
                    
                    //数据返回正常
                    if (isSuccessed) {
                        NSArray *requestData = [[dict objectForKey:@"data"] objectForKey:@"result"];
                        if (isRefresh.boolValue) {
                            self.listData = requestData;
                        } else {
                            self.listData = [self.listData arrayByAddingObjectsFromArray:requestData];
                        }
                        self.moreTime = [[dict objectForKey:@"data"] objectForKey:@"moreTime"];
                        self.totalPage = [[[[dict objectForKey:@"data"] objectForKey:@"pagination"] objectForKey:@"totalPage"] integerValue];
                    } else {
                        [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:100 userInfo:@{NSLocalizedDescriptionKey:errorMessage}]];
                    }
                    [subscriber sendNext:@(1)];
                    [subscriber sendCompleted];
                }];
            }
            return nil;
        }];
    }];
}

- (RACSignal *)getSelectedTypeArray {
    @weakify(self);
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        if (self.nameList) {
            [subscriber sendNext:nil];
            [subscriber sendCompleted];
            return nil;
        }
    
        [BusinessCircleRequestManager requesFindDictionaryInfoWithCode:@"ECOMM_TYPE_CODE" finished:^(id result, NSError* error) {
            if (error) {
                [subscriber sendError:error];
            }else {
                NSDictionary* resultDic = result;
                self.nameList = [resultDic objectForKey:@"nameList"];
                self.valueList = [resultDic objectForKey:@"valueList"];
                [self.nameList insertObject:@"全部" atIndex:0];
                [self.valueList insertObject:@"" atIndex:0];
                [subscriber sendNext:nil];
            }
            [subscriber sendCompleted];
        }];
        return nil;
    }];
    return signal;
}
@end
